import {UserActions} from './user';

export const PROVINCE = [
  {
    id: 'Kuala Lumpur',
    name: 'Kuala Lumpur',
  },
  {
    id: 'Johor',
    name: 'Johor',
  },
  {
    id: 'Kedah',
    name: 'Kedah',
  },
  {
    id: 'Kelantan',
    name: 'Kelantan',
  },
  {
    id: 'Labuan',
    name: 'Labuan',
  },
  {
    id: 'Melaka',
    name: 'Melaka',
  },
  {
    id: 'Negeri Sembilan',
    name: 'Negeri Sembilan',
  },
  {
    id: 'Pahang',
    name: 'Pahang',
  },
  {
    id: 'Penang',
    name: 'Penang',
  },
  {
    id: 'Perak',
    name: 'Perak',
  },
  {
    id: 'Perlis',
    name: 'Perlis',
  },
  {
    id: 'Sabah',
    name: 'Sabah',
  },
  {
    id: 'Sarawakr',
    name: 'Sarawakr',
  },
  {
    id: 'Selangor',
    name: 'Selangor',
  },
  {
    id: 'Terengganu',
    name: 'Terengganu',
  },
];

const initialState = {
  keywords: '',
  type: '',
  product: '',
  ordering: '',
  region: '',
  avail: '',
};

export const FilterParamsActions = {
  FILTER_PARAMS_UPDATE: 'filter_params_update',
  FILTER_PARAMS_REMOVE: 'filter_params_remove',
  FILTER_PARAMS_RESET: 'filter_params_reset',
};

export function updateFilterParams(payload) {
  return {
    type: FilterParamsActions.FILTER_PARAMS_UPDATE,
    payload,
  };
}

export function removeFilterParams(payload) {
  return {
    type: FilterParamsActions.FILTER_PARAMS_REMOVE,
    payload,
  };
}

export function resetFilterParams() {
  return {
    type: FilterParamsActions.FILTER_PARAMS_RESET,
  };
}

export default function filterParams(state = initialState, action) {
  switch (action.type) {
    case FilterParamsActions.FILTER_PARAMS_UPDATE:
      return {...state, ...action.payload};
    case FilterParamsActions.FILTER_PARAMS_REMOVE:
      delete state[action.payload.field];
      return {...state};
    case FilterParamsActions.FILTER_PARAMS_RESET:
      return {
        keywords: state.keywords,
        ordering: state.ordering,
        avail: state.avail,
        type: '',
        product: '',
        region: '',
      };
    case UserActions.USER_SIGN_OUT:
      return {
        keywords: '',
        type: '',
        product: '',
        ordering: '',
        region: '',
        avail: '',
      };
    default:
      return state;
  }
}
